//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
    public class FormatScheme : IOfficeXmlSerializable
    {
        private List<FillProperties> backgroundFillStyles = new List<FillProperties>();
        private List<EffectStyleItem> effectStyles = new List<EffectStyleItem>();
        private List<FillProperties> fillStyles = new List<FillProperties>();
        private List<LineProperties> lineStyles = new List<LineProperties>();
        private string name = "";

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public List<FillProperties> BackgroundFillStyles
        {
            get { return backgroundFillStyles; }
            set { backgroundFillStyles = value; }
        }


        public List<FillProperties> FillStyles
        {
            get { return fillStyles; }
            set { fillStyles = value; }
        }

        public List<LineProperties> LineStyles
        {
            get { return lineStyles; }
        }

        public List<EffectStyleItem> EffectStyles
        {
            get { return effectStyles; }
            set { effectStyles = value; }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("fmtScheme"))
            {
                name = c.GetOptionalString("name");
                if (name == null)
                    Name = "";
                using (ReadContext f = c.Read("fillStyleLst"))
                {
                    FillProperties p = FillProperties.ReadFillProperties(f);
                    {
                        while (p != null)
                        {
                            FillStyles.Add(p);
                            p = FillProperties.ReadFillProperties(f);
                        }
                    }
                }
                lineStyles = c.ReadOptionalListElement<LineProperties>("lnStyleLst", "ln");
                using (ReadContext ef = c.Read("effectStyleLst"))
                {
                    ef.PassAttributes();
                    while (ef.ReaderLocalName == "effectStyle")
                    {
                        EffectStyles.Add(ef.ReadElement<EffectStyleItem>());
                    }
                }

                using (ReadContext bg = c.Read("bgFillStyleLst"))
                {
                    FillProperties f = FillProperties.ReadFillProperties(bg);
                    while (f != null)
                    {
                        BackgroundFillStyles.Add(f);
                        f = FillProperties.ReadFillProperties(bg);
                    }
                }
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("fmtScheme"))
            {
                c.SetOptionalString("name", name);
                if (name == null)
                    Name = "";
                using (WriteContext f = c.Write("fillStyleLst"))
                {
                    foreach (FillProperties p in fillStyles)
                        p.WriteXmlCore(f);
                }
                if (lineStyles != null)
                    using (WriteContext c2 = c.Write("lnStyleLst"))
                    {
                        foreach (LineProperties pp in LineStyles)
                            c2.WriteElement(pp);
                    }
                using (WriteContext ef = c.Write("effectStyleLst"))
                {
                    foreach (EffectStyleItem i in EffectStyles)
                        ef.WriteElement(i);
                }

                using (WriteContext bg = c.Write("bgFillStyleLst"))
                {
                    foreach (FillProperties f in BackgroundFillStyles)
                    {
                        bg.WriteElement(f);
                    }
                }
            }
        }

        #endregion
    }
}